package com.sunhao.gulimall.gateway.filters;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

/**
 * 描述：MyCustomFilter 类的用途
 * 日期：2024/11/27
 */
@Component
public class MyCustomFilter implements GatewayFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取重写后的请求 URL
        URI rewrittenUri = exchange.getRequest().getURI();
        System.out.println("Rewritten URL: " + rewrittenUri.toString());

        // 调用下一个过滤器
        return chain.filter(exchange)
                .then(Mono.fromRunnable(() -> {
                    // 你可以在过滤器链执行后再次获取修改后的 URL
                    URI finalUri = exchange.getRequest().getURI();
                    System.out.println("Final URL after processing: " + finalUri.toString());
                }));
    }

    @Override
    public int getOrder() {
        // 返回过滤器的执行顺序
        return 0;
    }
}
